//
//  ViewController.m
//  UI_15_UICollectionViewLifeCycle
//
//  Created by li cheng on 16/8/3.
//  Copyright © 2016年 li cheng. All rights reserved.
//

#import "ViewController.h"
#import "MyCollectionViewCell.h"
@interface ViewController ()<UICollectionViewDelegate, UICollectionViewDataSource>

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
    layout.itemSize = CGSizeMake( (CGRectGetWidth(self.view.bounds) - 15) / 2, 100);
    layout.minimumLineSpacing = 5;
    layout.minimumInteritemSpacing = 5;
    layout.sectionInset = UIEdgeInsetsMake(5, 5, 5, 5);
    UICollectionView *collectionView = [[UICollectionView alloc] initWithFrame:self.view.bounds collectionViewLayout:layout];
    collectionView.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:collectionView];
    collectionView.delegate = self;
    collectionView.dataSource = self;
    [collectionView registerClass:[MyCollectionViewCell class] forCellWithReuseIdentifier:@"MyCollectionViewCell"];
    // 关于CollectionView LifeCycle
    // 1.numberOfSectionsInCollectionView
    // 2.collectionView:numberOfItemsInSection:
    // 注意:3-4循环
    // 3.prepareForReuse(从重用池取出时调用)
    // 4.collectionView:cellForItemAtIndexPath:
    // 5.collectionView:willDisplayCell:forItemAtIndexPath:
    // 当cell完全出屏幕时
    // 6.collectionView:didEndDisplayingCell:forItemAtIndexPath:
    // iOS 10以前
    // 当屏幕立即返回,依旧进入3->4->5
    // iOS 10以后
    // 出屏幕立即返回, 会执行4->5,不进入重用池
}
// 区数
- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView{
    NSLog(@"[%@:%s]", self, __FUNCTION__);
    return 1;
}
// 区里的row数
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    NSLog(@"[%@:%s]", self, __FUNCTION__);
    return 20;
}
// 重用池取出cell
- (__kindof UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    MyCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"MyCollectionViewCell" forIndexPath:indexPath];
    cell.backgroundColor = [UIColor colorWithRed:arc4random()%256 / 255.0f green:arc4random()%256 / 255.0f blue:arc4random()%256 / 255.0f alpha:1];
    NSLog(@"%@-----------%ld---------[%@:%s]", cell, indexPath.row, self, __FUNCTION__);
    return cell;
}
// iOS 8.0以后推出,cell将要离开屏幕时
- (void)collectionView:(UICollectionView *)collectionView willDisplayCell:(nonnull UICollectionViewCell *)cell forItemAtIndexPath:(nonnull NSIndexPath *)indexPath{
    NSLog(@"%@-----------%ld---------[%@:%s]", cell, indexPath.row, self, __FUNCTION__);
}
//
- (void)collectionView:(UICollectionView *)collectionView didEndDisplayingCell:(nonnull UICollectionViewCell *)cell forItemAtIndexPath:(nonnull NSIndexPath *)indexPath{
    NSLog(@"%@-----------%ld---------[%@:%s]", cell, indexPath.row, self, __FUNCTION__);
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


@end
